package com.author.menubar;

import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.Action;
import javax.swing.JMenu;
import javax.swing.JMenuItem;
import javax.swing.KeyStroke;
import javax.swing.event.UndoableEditListener;
import javax.swing.text.DefaultEditorKit;
import javax.swing.text.Document;
import javax.swing.text.EditorKit;
import javax.swing.text.html.HTMLDocument;
import javax.swing.text.html.HTMLEditorKit;
import javax.swing.undo.UndoManager;

import com.author.document.model.ExtendedDocument;
import com.author.surface.TextPane;

public class EditMenu extends JMenu {
	/** Undo */
	private JMenuItem	undoMenuItem		= new JMenuItem("Undo");

	/** Redo */
	private JMenuItem	redoMenuItem		= new JMenuItem("Redo");

	/** Cut */
	private JMenuItem	cutMenuItem			= new JMenuItem("Cut");

	/** Copy */
	private JMenuItem	copyMenuItem		= new JMenuItem("Copy");

	/** Paste */
	private JMenuItem	pasteMenuItem		= new JMenuItem("Paste");

	/** Select All */
	private JMenuItem	selectAllMenuItem	= new JMenuItem("Select All");

	/** Delete */
	private JMenuItem	deleteMenuItem		= new JMenuItem("Delete");

	public EditMenu(String menuname) {
		super(menuname);

		/* Add Undo/Redo menu items for Edit Menu */
		this.add(undoMenuItem);
		undoMenuItem.setMnemonic('U');
		undoMenuItem.setAccelerator(KeyStroke.getKeyStroke(
				java.awt.event.KeyEvent.VK_Z, java.awt.Event.CTRL_MASK));

		Document document = TextPane.getTextPaneDocument();
		if (document instanceof ExtendedDocument) {
			final UndoManager undo = (UndoManager) ((ExtendedDocument) document)
					.getUndoManager();
			undoMenuItem.addActionListener(new ActionListener() {
				@Override
				public void actionPerformed(ActionEvent e) {
					if (e == null) {
						return;
					}

					if (!(e.getSource() instanceof JMenuItem)) {
						return;
					}

					/* Undo action */
					undo.undo();
				}
			});
		}

		this.add(redoMenuItem);
		redoMenuItem.setMnemonic('R');
		redoMenuItem.setAccelerator(KeyStroke.getKeyStroke(
				java.awt.event.KeyEvent.VK_Y, java.awt.Event.CTRL_MASK));

		if (document instanceof ExtendedDocument) {
			final UndoManager undo = (UndoManager) ((ExtendedDocument) document)
					.getUndoManager();
			redoMenuItem.addActionListener(new ActionListener() {
				@Override
				public void actionPerformed(ActionEvent e) {
					if (e == null) {
						return;
					}

					if (!(e.getSource() instanceof JMenuItem)) {
						return;
					}

					/* Redo action */
					undo.redo();
				}
			});
		}

		/* Add item separator */
		this.addSeparator();

		/* Add Cut/copy/paste */
		this.add(cutMenuItem);
		cutMenuItem.setMnemonic('T');
		cutMenuItem.setAccelerator(KeyStroke.getKeyStroke(
				java.awt.event.KeyEvent.VK_X, java.awt.Event.CTRL_MASK));

		EditorKit editorKit = TextPane.getTextPaneEditorKit();
		if (editorKit instanceof HTMLEditorKit) {

			/*
			 * Search the editor kit for the cut action and add it to the edit
			 * menu item
			 */
			Action[] actions = editorKit.getActions();
			for (int i = 0; i < actions.length; i++) {
				if (actions[i].getValue(Action.NAME).equals(
						DefaultEditorKit.cutAction)) {
					this.cutMenuItem.addActionListener(actions[i]);
				}
			}
		}

		this.add(copyMenuItem);
		copyMenuItem.setMnemonic('C');
		copyMenuItem.setAccelerator(KeyStroke.getKeyStroke(
				java.awt.event.KeyEvent.VK_C, java.awt.Event.CTRL_MASK));

		if (editorKit instanceof HTMLEditorKit) {

			/*
			 * Search the editor kit for the copy action and add it to the edit
			 * menu item
			 */
			Action[] actions = editorKit.getActions();
			for (int i = 0; i < actions.length; i++) {
				if (actions[i].getValue(Action.NAME).equals(
						DefaultEditorKit.copyAction)) {
					this.copyMenuItem.addActionListener(actions[i]);
				}
			}
		}

		this.add(pasteMenuItem);
		pasteMenuItem.setMnemonic('P');
		pasteMenuItem.setAccelerator(KeyStroke.getKeyStroke(
				java.awt.event.KeyEvent.VK_V, java.awt.Event.CTRL_MASK));

		if (editorKit instanceof HTMLEditorKit) {

			/*
			 * Search the editor kit for the paste action and add it to the edit
			 * menu item
			 */
			Action[] actions = editorKit.getActions();
			for (int i = 0; i < actions.length; i++) {
				if (actions[i].getValue(Action.NAME).equals(
						DefaultEditorKit.pasteAction)) {
					this.pasteMenuItem.addActionListener(actions[i]);
				}
			}
		}

		this.addSeparator();

		/* Add Select All and Delete */
		this.add(selectAllMenuItem);
		selectAllMenuItem.setMnemonic('A');
		selectAllMenuItem.setAccelerator(KeyStroke.getKeyStroke(
				java.awt.event.KeyEvent.VK_A, java.awt.Event.CTRL_MASK));

		if (editorKit instanceof HTMLEditorKit) {

			/*
			 * Search the editor kit for the select all action and add it to the
			 * edit menu item
			 */
			Action[] actions = editorKit.getActions();
			for (int i = 0; i < actions.length; i++) {
				if (actions[i].getValue(Action.NAME).equals(
						DefaultEditorKit.selectAllAction)) {
					this.selectAllMenuItem.addActionListener(actions[i]);
				}
			}
		}

		this.add(deleteMenuItem);
		if (editorKit instanceof HTMLEditorKit) {

			/*
			 * Search the editor kit for the delete action and add it to the
			 * edit menu item
			 */
			Action[] actions = editorKit.getActions();
			for (int i = 0; i < actions.length; i++) {
				if (actions[i].getValue(Action.NAME).equals(
						DefaultEditorKit.deletePrevCharAction)) {
					this.deleteMenuItem.addActionListener(actions[i]);
				}
			}
		}
	}
}
